package com.leyou.order.web;

import com.leyou.order.dto.OrderDTO;
import com.leyou.order.pojo.Order;
import com.leyou.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Map<String, String>> createOrder(@RequestBody OrderDTO orderDTO) {
        Long id = orderService.createOrder(orderDTO);
        log.info("[创建订单] 订单已创建，返回 id：{}", id);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id.toString());
        return ResponseEntity.ok(map);
    }

    /**
     * 根据 id 查询订单信息
     * @param orderId   订单 id
     * @return          Order 对象
     */
    @GetMapping("{orderId}")
    public ResponseEntity<Order> queryOrderById(@PathVariable("orderId") Long orderId) {
        return ResponseEntity.ok(orderService.queryOrderById(orderId));
    }

    /**
     * 创建微信支付链接
     * @param orderId   订单 id
     * @return          微信支付 url
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> ceratePayUrl(@PathVariable("id") Long orderId) {
        return ResponseEntity.ok(orderService.createPayUrl(orderId));
    }

    @GetMapping("/state/{id}")
    public ResponseEntity<Integer> queryOrderStatus(@PathVariable("id") Long orderId) {
        return ResponseEntity.ok(orderService.queryOrderState(orderId).getValue());
    }
}














